package net.begincode.service.down.impl;

import java.util.ArrayList;
import java.util.List;

import utils.CacheUtils;
import config.DbTemplate;
import net.begincode.bean.BcDown;
import net.begincode.bean.CourseChapter;
import net.begincode.service.down.BcDownService;

public class BcDownServiceImpl implements BcDownService {
	private BcDownServiceImpl() {
	}

	private static BcDownServiceImpl instance = null;

	public static BcDownServiceImpl getInstance() {
		if (instance == null) {
			synchronized (BcDownServiceImpl.class) {
				if (instance == null) {
					instance = new BcDownServiceImpl();
				}
			}
		}
		return instance;
	}

	@Override
	public int addBcDown(BcDown bcDown) {
		
		StringBuilder addSql = new StringBuilder();
		addSql.append(" insert into bc_down (down_name,down_abstract,down_intro,down_count,down_addr,cdate,key_word,bc_navigation_id,check_flag) ");
		addSql.append(" values(?,?,?,?,?,now(),?,?,?) ");
		return DbTemplate.insert(addSql.toString(), new Object[]{
			bcDown.getDown_name(),bcDown.getDown_abstract(),bcDown.getDown_intro(),bcDown.getDown_count(),bcDown.getDown_addr(),bcDown.getKey_word(),bcDown.getBc_navigation_id(),bcDown.getCheck_flag()
		});
	}

	@Override
	public List<BcDown> selDownLimitN(int navId, int n) {
		
		String selSql = " select * from bc_down where bc_navigation_id = ?  order by cdate desc limit ? ";
		return DbTemplate.queryForList(selSql,new Object[]{navId,n}, BcDown.class);
	}

	@Override
	public List<BcDown> selDownByParamLimitN(String courseId, int navId,
			int n) {
		
		String selSql  = "select bd.* from chapter_down_rel cdr left join course_chapter cc on cdr.course_chapter_id = cc.course_chapter_id left join  bc_down bd on cdr.bc_down_id = bd.bc_down_id where cc.bc_course_id = ? and bd.bc_navigation_id = ? order by cdate desc limit ?";
		return DbTemplate.queryForList(selSql, new Object[]{courseId,navId,n}, BcDown.class);
	}

	@Override
	public List<BcDown> selDownByNavId(String navId) {
		
		String selSql =" select * from  bc_down where bc_navigation_id = ? order by cdate desc   ";
		return DbTemplate.queryForList(selSql, new Object[]{navId}, BcDown.class);
	}

	@Override
	public List<BcDown> selHotDownLimit(int navId, int n) {
		
		String selSql = " select * from bc_down where bc_navigation_id = ? order by down_count desc  limit ? ";
		return DbTemplate.queryForList(selSql,new Object[]{navId,n}, BcDown.class);
	}

	@Override
	public BcDown selDownById(String id) {
		
		String selSql = " select * from bc_down where bc_down_id = ?  ";
		return DbTemplate.queryForObject(selSql, new Object[]{id}, BcDown.class); 
	}

	@Override
	public List<BcDown> selDownByParam(BcDown bcDown) {
		
		String selSql = " select * from bc_down  ";
		return DbTemplate.queryForList(selSql, null, BcDown.class);
	}

	@Override
	public int updDown(BcDown bcDown) {
		List paraList = new ArrayList();
		StringBuilder updSql = new StringBuilder();
		updSql.append(" update  bc_down set down_name = ? ");
		paraList.add(bcDown.getDown_name());
		if(bcDown.getDown_abstract() != null){
			updSql.append(" ,down_abstract = ? ");
			paraList.add(bcDown.getDown_abstract());
		}
		if(bcDown.getDown_intro() != null){
			updSql.append(" ,down_intro = ? ");
			paraList.add(bcDown.getDown_intro());
		}
		if(bcDown.getDown_count() != 0){
			updSql.append(" ,down_count = ? ");
			paraList.add(bcDown.getDown_count());
		}
		if(bcDown.getDown_addr() != null){
			updSql.append(" ,down_addr = ? ");
			paraList.add(bcDown.getDown_addr());
		}
		if(bcDown.getKey_word()!= null){
			updSql.append(" ,key_word = ? ");
			paraList.add(bcDown.getKey_word());
		}
		if(bcDown.getBc_navigation_id() != 0){
			updSql.append(" ,bc_navigation_id = ? ");
			paraList.add(bcDown.getBc_navigation_id());
		}
		if(bcDown.getCheck_flag() != null){
			updSql.append(" ,check_flag = ? ");
			paraList.add(bcDown.getCheck_flag());
		}
		updSql.append(" where bc_down_id = ? ");
		paraList.add(bcDown.getBc_down_id());
		return DbTemplate.update(updSql.toString(), paraList.toArray());
	}

	@Override
	public BcDown selDownByIdCache(String id) {
		
		String selSql = " select * from bc_down where bc_down_id = ?  ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.DOWN_CACHE, id);
		if(dbEle != null){
			return (BcDown)dbEle;
		}else{
			BcDown ccs = DbTemplate.queryForObject(selSql, new Object[]{id}, BcDown.class);
			CacheUtils.addEleByParam(CacheUtils.DOWN_CACHE, id, ccs);
			return ccs;
		}
		
	}

	@Override
	public List<BcDown> selDownByNavIdCache(String navId) {
		
		String selSql =" select * from  bc_down where bc_navigation_id = ? order by cdate desc   ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.DOWN_CACHE, "navDowns"+navId);
		if(dbEle != null){
			return (List<BcDown>)dbEle;
		}else{
			List<BcDown> ccs = DbTemplate.queryForList(selSql, new Object[]{navId}, BcDown.class);
			CacheUtils.addEleByParam(CacheUtils.DOWN_CACHE, "navDowns"+navId, ccs);
			return ccs;
		}
	}

	@Override
	public List<BcDown> selDownByParamLimitNCache(String courseId, int navId,
			int n) {
		
		String selSql  = "select bd.* from chapter_down_rel cdr left join course_chapter cc on cdr.course_chapter_id = cc.course_chapter_id left join  bc_down bd on cdr.bc_down_id = bd.bc_down_id where cc.bc_course_id = ? and bd.bc_navigation_id = ? order by cdate desc limit ?";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.DOWN_CACHE, "downParam"+navId+courseId);
		if(dbEle != null){
			return (List<BcDown>)dbEle;
		}else{
			List<BcDown> ccs = DbTemplate.queryForList(selSql, new Object[]{courseId,navId,n}, BcDown.class);
			CacheUtils.addEleByParam(CacheUtils.DOWN_CACHE, "downParam"+navId+courseId, ccs);
			return ccs;
		}
	}

	@Override
	public List<BcDown> selDownLimitNCache(int navId, int n) {
		
		String selSql = " select * from bc_down where bc_navigation_id = ?  order by cdate desc limit ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.DOWN_CACHE, "downlimitN"+navId);
		if(dbEle != null){
			return (List<BcDown>)dbEle;
		}else{
			List<BcDown> ccs = DbTemplate.queryForList(selSql,new Object[]{navId,n}, BcDown.class);
			CacheUtils.addEleByParam(CacheUtils.DOWN_CACHE, "downlimitN"+navId, ccs);
			return ccs;
		}
	}

	@Override
	public List<BcDown> selHotDownLimitCache(int navId, int n) {
		
		String selSql = " select * from bc_down where bc_navigation_id = ? order by down_count desc  limit ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.DOWN_CACHE, "hotDown"+navId);
		if(dbEle != null){
			return (List<BcDown>)dbEle;
		}else{
			List<BcDown> ccs = DbTemplate.queryForList(selSql,new Object[]{navId,n}, BcDown.class);
			CacheUtils.addEleByParam(CacheUtils.DOWN_CACHE, "hotDown"+navId, ccs);
			return ccs;
		}
	}

	@Override
	public void updDownViewCount(String id, int count) {
		String updSql = " update bc_down set down_count = down_count + ? where bc_down_id = ? ";
		DbTemplate.update(updSql, new Object[]{count,id});
	}
}
